package com.example.tally;

import android.graphics.Color;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import com.example.tally.adapter.CharVPAdapter;
import com.example.tally.db.DBManager;
import com.example.tally.frag_char.IncomeCharFragment;
import com.example.tally.frag_char.OutcomeCharFragment;
import com.example.tally.utils.CalendarDialog;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class MonthCharActivity extends AppCompatActivity {
    TextView dateTv,inTv,outTv;
    ViewPager charTv;
    Button btn_out , btn_in;
    private int year;
    private int month;
    int selectPos = -1, selectMonth = -1;
    List<Fragment> charFragList;
    private IncomeCharFragment incomeCharFragment;
    private OutcomeCharFragment outcomeCharFragment;
    private CharVPAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_month_char);
        initView();
        initTime();
        initDatas(year , month);
        initFrag();
        setVPSelectListener();
    }

    /**为页面添加滑动监听*/
    private void setVPSelectListener() {
        charTv.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                setBtnStyle(position);
            }
        });
    }

    private void initFrag() {
        charFragList = new ArrayList<>();
        incomeCharFragment = new IncomeCharFragment();
        outcomeCharFragment = new OutcomeCharFragment();
        // 使用Bundle传数据
        Bundle bundle = new Bundle();
        bundle.putInt("year" , year);
        bundle.putInt("month" , month);
        incomeCharFragment.setArguments(bundle);
        outcomeCharFragment.setArguments(bundle);
        // 将Fragment对象存到数据源中
        charFragList.add(outcomeCharFragment);
        charFragList.add(incomeCharFragment);
        // 使用适配器
        adapter = new CharVPAdapter(getSupportFragmentManager(), charFragList);
        charTv.setAdapter(adapter);
        // 将Fragment加载到Activity中
    }

    private void initDatas(int year, int month) {
        float inMoneyOneMonth = DBManager.setSumMoneyOneMonth(year, month, 1);// 收入总金额
        float outMoneyOneMonth = DBManager.setSumMoneyOneMonth(year, month, 0);// 支出总金额
        int incountItemOneMonth = DBManager.getCountItemOneMonth(year, month, 1);// 收入总笔数
        int outcountItemOneMonth = DBManager.getCountItemOneMonth(year, month, 0);// 支出总笔数
        dateTv.setText(year + "年" + month + "月份账单");
        inTv.setText("共" + incountItemOneMonth + "笔收入，￥ " + inMoneyOneMonth);
        outTv.setText("共" + outcountItemOneMonth + "笔支出，￥ " + outMoneyOneMonth);

    }

    /**初始化时间，注意：Calendar中，Calendar.get(Calendar.MONTH)得到的是真实月份 - 1*/
    private void initTime() {
        Calendar calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH) + 1;
    }

    private void initView() {
        dateTv = findViewById(R.id.char_tv_date);
        inTv = findViewById(R.id.char_tv_in);
        outTv = findViewById(R.id.char_tv_out);
        charTv = findViewById(R.id.char_vp);
        btn_in = findViewById(R.id.char_btn_in);
        btn_out = findViewById(R.id.char_btn_out);
    }
    public void onClick(View v){
        switch (v.getId()){
            case R.id.char_iv_back:
                finish();
                break;
            case R.id.char_iv_rill:
                showCalendarDialog();
                break;
            case R.id.char_btn_in:
                setBtnStyle(1);
                charTv.setCurrentItem(1);
                break;
            case R.id.char_btn_out:
                setBtnStyle(0);
                charTv.setCurrentItem(0);
                break;
        }
    }

    /**显示日历对话框*/
    private void showCalendarDialog() {
        CalendarDialog dialog = new CalendarDialog(this, selectPos, selectMonth);
        dialog.show();
        dialog.setDialogSize();
        dialog.setOnRefreshListener(new CalendarDialog.OnRefreshListener() {
            @Override
            public void Ensure(int selPos, int year, int month) {
                // 为了下一次进去的时候还是上次点击的位置，所以需要将数据来回传递
                MonthCharActivity.this.selectPos = selPos;
                MonthCharActivity.this.selectMonth = month;
                initDatas(year , month);
                incomeCharFragment.setDate(year , month);
                outcomeCharFragment.setDate(year , month);
            }
        });
    }

    /**Button样式会发生变化 支出——>0 ，收入——>1，kind的值关联支出或者收入*/
    private void setBtnStyle(int kind){
        if(kind == 0){
            btn_out.setBackgroundResource(R.drawable.main_recordbtn_bg);
            btn_out.setTextColor(Color.WHITE);
            btn_in.setBackgroundResource(R.drawable.dialog_btn_bg);
            btn_in.setTextColor(Color.BLACK);
        }else{
            btn_in.setBackgroundResource(R.drawable.main_recordbtn_bg);
            btn_in.setTextColor(Color.WHITE);
            btn_out.setBackgroundResource(R.drawable.dialog_btn_bg);
            btn_out.setTextColor(Color.BLACK);
        }
    }

}